package com.aaa.service.impl;

import com.aaa.vo.UserVo;

import com.aaa.utils.JwtUtil;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.aaa.dao.UserDao;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public Result<User> getUserInfo() {
        String token = WebUtil.getRequest().getHeader("token");
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        //根据手机号查询
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",map.get("username"));
        wrapper.eq("status",0);
        User user = userDao.selectOne(wrapper);
        return new Result<User>(2000,"查询用户成功",user);
    }
    @Override
    public User getByUsername(String username) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("phone", username);
        qw.eq("status", 0);
        return userDao.selectOne(qw);
    }
    @Override
    public List<User> getListAllUser(Map<String, Object> map) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(map.get("userId"))) {
            wrapper.eq("user_Id", map.get("userId"));
        }
        if (Objects.nonNull(map.get("deptId"))) {
            wrapper.eq("dept_id", map.get("deptId"));
        }
        wrapper.eq("scheduling_flag", 0);
        wrapper.eq("status", 0);
        return userDao.selectList(wrapper);
    }
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public Result<String> loginOut() {
        String token = WebUtil.getRequest().getHeader("token");
        try {
            Boolean token1 = redisTemplate.delete(token);
            System.out.println("token1 = " + token1);
            if (token1){
                return new Result<>(2000,"ok");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> getListUser() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("scheduling_flag", 0);
        wrapper.eq("status", 0);
        return userDao.selectList(wrapper);
    }

    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",username);
        wrapper.eq("status",0);
        return userDao.selectOne(wrapper);
    }

    @Override
    public Result<IPage<User>> findByConditions(Integer currentPage, Integer pageSize, UserVo userVo) {
            IPage<User> page=new Page<>(currentPage,pageSize);
            QueryWrapper<User> wrapper=new QueryWrapper<>();
            if(StringUtils.hasText(userVo.getUserName())){
                wrapper.like("user_name",userVo.getUserName());
            }
            if(StringUtils.hasText(userVo.getPhone())){
                wrapper.eq("phone",userVo.getPhone());
            }
            if(Objects.nonNull(userVo.getDeptId())){
                wrapper.eq("dept_id",userVo.getDeptId());
            }
            if(Objects.nonNull(userVo.getStatus())){
                wrapper.eq("status",userVo.getStatus());
            }
            if(Objects.nonNull(userVo.getDateRange())&&userVo.getDateRange().length==2){
                wrapper.between("create_time",userVo.getDateRange()[0],userVo.getDateRange()[1]);
            }

            IPage<User> page1 = userDao.selectPage(page, wrapper);
            return new Result<IPage<User>>(2000,"查询用户成功",page1);
        }

    @Override
    public boolean delById(Long id) {
        int i = userDao.deleteById(id);
        if (i > 0) {
            return true;
        }
            return false;
    }

    @Override
    public boolean saveAndUpdate(User user) {
        int i=-1;
        if (user.getCreateTime()==null){
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
        }else {
            user.setUpdateTime(new Date());
        }
        if(user.getUserId()==null){
            i=userDao.insert(user);
            System.out.println("====="+user.getStatus());
        }else {
            i=userDao.updateById(user);
        }
        if (i>0){
            return true;
        }
        return false;
    }

}
